package cn.neusoft.neusoft_fz.controller;

import cn.neusoft.neusoft_fz.common.ResponseEntity;
import cn.neusoft.neusoft_fz.service.DeliveryPersonService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.DeliveryOrder;
import org.example.entity.DeliveryPerson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/delivery-person")
public class DeliveryPersonController {
    @Autowired
    private DeliveryPersonService deliveryPersonService;

    @PostMapping
    @RequestMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<DeliveryPerson> addDp(@RequestBody DeliveryPerson deliveryPerson){
        log.info(deliveryPerson.toString());
        deliveryPersonService.save(deliveryPerson);
        return ResponseEntity.success(deliveryPerson);
    }

    @GetMapping
    @RequestMapping("/all")
    public ResponseEntity<List<DeliveryPerson>> getAll(){
        List<DeliveryPerson> list = deliveryPersonService.list();
        return ResponseEntity.success(list);
    }


    @GetMapping
    @RequestMapping("/all/{subid}")
    public ResponseEntity<List<DeliveryPerson>> getAllBySubid(@PathVariable String subid){
        LambdaQueryWrapper<DeliveryPerson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeliveryPerson::getSubstationId,subid);
        List<DeliveryPerson> list = deliveryPersonService.list(queryWrapper);
        return ResponseEntity.success(list);
    }


    @GetMapping
    @RequestMapping("/findById/{id}")
    public ResponseEntity<DeliveryPerson> getByid(@PathVariable Long id){
        DeliveryPerson deliveryPerson = deliveryPersonService.getById(id);
        return ResponseEntity.success(deliveryPerson);
    }



}
